package com.gomore.opple.module.redpackage;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentStatePagerAdapter;
import android.view.ViewGroup;
import android.widget.RadioButton;
import android.widget.RadioGroup;

import com.gomore.opple.R;
import com.gomore.opple.module.BaseActivity;
import com.gomore.opple.module.BaseFragmentV4;
import com.gomore.opple.module.redpackage.initial.InitialFragment;
import com.gomore.opple.module.redpackage.initial.InitialPresenter;
import com.gomore.opple.module.redpackage.initial.InitialPresenterModule;
import com.gomore.opple.module.redpackage.refuse.RefuseFragment;
import com.gomore.opple.module.redpackage.refuse.RefusePresenter;
import com.gomore.opple.module.redpackage.refuse.RefusePresenterModule;
import com.gomore.opple.module.redpackage.used.UsedFragment;
import com.gomore.opple.module.redpackage.used.UsedPresenter;
import com.gomore.opple.module.redpackage.used.UsedPresenterModule;
import com.gomore.opple.widgets.MyViewPager;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

import butterknife.Bind;

/**
 * Created by asus on 2017/5/9.
 */
public class RedPackageActivity extends BaseActivity {
    private static final int PAGE_COUNT = 3;

    @Inject
    InitialPresenter initialPresenter;
    @Inject
    RefusePresenter refusePresenter;
    @Inject
    UsedPresenter usedPresenter;

    InitialFragment initialFragment;
    RefuseFragment refuseFragment;
    UsedFragment usedFragment;

    @Bind(R.id.bottom_menu)
    RadioGroup bottom_menu;
    @Bind(R.id.rb_initial)
    RadioButton rb_initial;
    @Bind(R.id.rb_refuse)
    RadioButton rb_refuse;
    @Bind(R.id.rb_used)
    RadioButton rb_used;

    @Bind(R.id.main_viewpager)
    MyViewPager main_viewpager;
    private List<BaseFragmentV4> fragmentV4List;

    @Override
    protected void initalizeViewData() {
        super.initalizeViewData();
        fragmentV4List = new ArrayList<>();
    }

    @Override
    protected void initalizeViews() {
        //初始化fragment对象
        if (initialFragment == null) {
            initialFragment = InitialFragment.getInstance();
        }
        fragmentV4List.add(initialFragment);
        if (refuseFragment == null) {
            refuseFragment = RefuseFragment.getInstance();
        }
        fragmentV4List.add(refuseFragment);
        if (usedFragment == null) {
            usedFragment = UsedFragment.getInstance();
        }
        fragmentV4List.add(usedFragment);
        main_viewpager.setOffscreenPageLimit(PAGE_COUNT);
        bottom_menu.setOnCheckedChangeListener(new RadioGroup.OnCheckedChangeListener() {
            @Override
            public void onCheckedChanged(RadioGroup group, int checkedId) {
                switch (checkedId) {
                    case R.id.rb_initial:
                        main_viewpager.setCurrentItem(0);
                        break;
                    case R.id.rb_refuse:
                        main_viewpager.setCurrentItem(1);
                        break;
                    case R.id.rb_used:
                        main_viewpager.setCurrentItem(2);
                        break;
                }
            }
        });
        bindViewPagerAndBottomMenu();
    }

    @Override
    protected void initializeDependencyInjector() {
        DaggerRedPackageComponent.builder()
                .dataRepositoryComponent(getRepositoryComponent())
                .initialPresenterModule(new InitialPresenterModule(initialFragment))
                .refusePresenterModule(new RefusePresenterModule(refuseFragment))
                .usedPresenterModule(new UsedPresenterModule(usedFragment))
                .build()
                .inject(this);
    }

    //ViewPager展示
    private void bindViewPagerAndBottomMenu() {
        MyFragmentPagerAdapter myFragmentPagerAdapter = new MyFragmentPagerAdapter(
                getSupportFragmentManager(), fragmentV4List);
        main_viewpager.setAdapter(myFragmentPagerAdapter);
        main_viewpager.setCurrentItem(0);
    }


    public class MyFragmentPagerAdapter extends FragmentStatePagerAdapter {
        List<BaseFragmentV4> fragments;

        public MyFragmentPagerAdapter(FragmentManager fm, List<BaseFragmentV4> list) {
            super(fm);
            this.fragments = list;
        }

        @Override
        public int getCount() {
            return fragments.size();
        }

        @Override
        public Fragment getItem(int position) {
            BaseFragmentV4 fragment = fragments.get(position);
            return fragment;
        }

        @Override
        public void destroyItem(ViewGroup container, int position, Object object) {
            super.destroyItem(container, position, object);
        }

        @Override
        public void setPrimaryItem(ViewGroup container, int position,
                                   Object object) {
            super.setPrimaryItem(container, position, object);
        }
    }

    @Override
    protected int getLayoutView() {
        return R.layout.activity_red_package;
    }

}
